﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Management.Instrumentation;
using System.Security.Cryptography;
using System.IO;
using System.Net;


namespace ElectronicWMSA.Comm.ClassFiles
{
    public static class PCInfoClass
    {
        public static string cpuInfo; //1.cpu序列号 
        public static string macAddress; //2.mac序列号 
        public static string diskID; //3.硬盘id 
        public static string ipAddress; //4.ip地址 
        public static string loginUserName; //5.登录用户名 
        public static string computerName; //6.计算机名 
        public static string systemType; //7.系统类型 
        public static string totalPhysicalMemory; //8.内存量 单位:M 

        /// <summary>
        ///   //1.获取CPU序列号代码
        /// </summary>
        /// <returns></returns>
        public static string GetCpuID()
        {
            try
            {
                cpuInfo = "";//cpu序列号  
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        ///  //2.获取网卡硬件地址 
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            try
            {
                macAddress = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        macAddress = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return macAddress;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        ///   //3.获取硬盘ID  
        /// </summary>
        /// <returns></returns>
        public static string GetDiskID()
        {
            try
            {
                ManagementObjectSearcher mos = new ManagementObjectSearcher();
                mos.Query = new SelectQuery("Win32_DiskDrive", "", new string[] { "PNPDeviceID", "Signature" });
                ManagementObjectCollection myCollection = mos.Get();
                ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator();
                em.MoveNext();
                ManagementBaseObject moo = em.Current;
                string id = moo.Properties["signature"].Value.ToString().Trim();
                return id;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        ///  //4.获取IP地址  
        /// </summary>
        /// <returns></returns>
        public static IPAddress[] GetLocalIP()
        {
            string name = Dns.GetHostName();
            IPHostEntry me = Dns.GetHostEntry(name);
            return me.AddressList;
        }
        /// <summary>
        /// IPAddress
        /// </summary>
        /// <returns></returns>
        public static IPAddress GetFirstIP()
        {
            IPAddress[] ips = GetLocalIP();
            foreach (IPAddress ip in ips)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    continue;
                return ip;
            }
            return ips != null && ips.Length > 0 ? ips[0] : new IPAddress(0x0);
        }


        /// <summary>
        ///  // 5.操作系统的登录用户名 
        /// </summary>
        /// <returns></returns>
        public static string GetUserName()
        {
            try
            {
                string loginUserName = "";
                loginUserName = Environment.UserName;
                return loginUserName;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        ///  //6.获取计算机名
        /// </summary>
        /// <returns></returns>
        public static string GetComputerName()
        {
            try
            {
                return System.Environment.MachineName;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        ///  //7. PC类型  
        /// </summary>
        /// <returns></returns>
        public static string GetSystemType()
        {
            try
            {
                systemType = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    systemType = mo["SystemType"].ToString();
                }
                moc = null;
                mc = null;
                return systemType;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        ///  // 8.物理内存  
        /// </summary>
        /// <returns></returns>
        public static string GetTotalPhysicalMemory()
        {
            try
            {
                totalPhysicalMemory = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    totalPhysicalMemory = mo["TotalPhysicalMemory"].ToString();
                }
                moc = null;
                mc = null;
                return totalPhysicalMemory;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        /// 生成机器码
        /// </summary>
        /// <returns></returns>
        public static string getMNum()
        {
            return GetCpuID() + GetDiskID();//获得24位Cpu和硬盘序列号
        }

        public static int[] intCode = new int[127];//存储密钥
        public static int[] intNumber = new int[25];//存机器码的Ascii值
        public static char[] Charcode = new char[25];//存储机器码字
        public static void setIntCode()//给数组赋值小于10的数
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }
        }

        /// <summary>
        /// 生成注册码
        /// </summary>
        /// <returns></returns>
        public static string getRNum(string pcCode)
        {
            setIntCode();//初始化127位数组
            string MNum = pcCode;//获取注册码
            for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
            {
                Charcode[i] = Convert.ToChar(MNum.Substring(i - 1, 1));
            }
            for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
            {
                intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
            }
            string strAsciiName = "";//用于存储注册码
            for (int j = 1; j < intNumber.Length; j++)
            {
                if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0－9之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A－Z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a－z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else//判断字符ASCII值不在以上范围内
                {
                    if (intNumber[j] > 122)//判断字符ASCII值是否大于z
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
                    }
                    else
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
                    }
                }
            }
            return strAsciiName;//返回注册码
        }

        #region  加密解密
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// 加密字符串
        /// </summary>
        public static string Encryption(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        public static string Decryption(string ryptString, string ryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(ryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(ryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return ryptString;
            }
        }
        #endregion

    }
}
